﻿using System.Windows.Media;
using System.Collections.Generic;
using System.Windows;
using VNet.VisiSuite.Common.Logger;
using VNet.VisiSuite.VisiBoard.Common.Helpers;
using VNet.VisiSuite.VisiBoard.Models;
using VNet.VisiSuite.Common.Helpers;
using VNet.VisiSuite.VisiBoard.Dal.EntityClasses;

namespace VNet.VisiSuite.VisiBoard.ViewModels
{
    public class DowntimeDialogViewModel : DialogViewModel
    {
        #region Fields
        private DowntimeDialogModel _model;
        private Visibility _visibility;
        private bool _visibox_event;
		private string _error;
        private OperatorEntity _operator = null;

        private string _label;
        private bool _is_break_lunch;

        public string Label
        {
            get { return _label; }
            set {
                _label = value;
                OnPropertyChanged("Label");
            }
        }

        public bool IsBreakLunch
        {
            get { return _is_break_lunch; }
            set
            {
                _is_break_lunch = value;
                OnPropertyChanged("IsBreakLunch");
                OnPropertyChanged("ReasonCodes");
            }
        }
        #endregion

        public DowntimeDialogViewModel()
        {
            _visibility = Visibility.Visible;
            Heading = "Downtime Reason";
            Label = "Select the downtime reason";
            Model = new DowntimeDialogModel();
        }

        #region Properties

        public DowntimeDialogModel Model
        {
            get { return _model; }
            set
            {
                _model = value;
                OnPropertyChanged("Model");
            }
        }

        public bool alarmColors(int percentage, out Brush foreground, out Brush background)
        {
            foreground = Brushes.White;
            background = Brushes.Red;
            var visi_board = BusinessLogic.VisiBoard.instance();

            string foreground_text;
            string background_text;
            var success = visi_board.alarmColors(percentage, false, out foreground_text,out background_text);
            if (success)
            {
                foreground = new SolidColorBrush(ColorHelper.StringToColor(foreground_text, Colors.White));
                background = new SolidColorBrush(ColorHelper.StringToColor(background_text, Colors.Red)); 
            }
            return success;
        }

        public int TotalSecondsProgressBar
        {
            get { return 55; }
        }

        public IEnumerable<SimpleNode> ReasonCodes
        {
            get
            {
                // Check if it is triggered by Break / Lunch button
                if (_is_break_lunch)
                    return _model.BreakReasonCodes;
                else
                    return _model.ReasonCodes;
            }
        }

        public string OperatorCode { get; set; }

        public OperatorEntity Operator
        {
            get { return _operator; }
        }

        public string ValidationError
        {
            get { return _error; }
            set
            {
                _error = value;
                OnPropertyChanged("ValidationError");
            }
        }


        public  void refresh()
        {
            FileLog.Error("DowntimeDialogViewModel.refresh()");
            OnPropertyChanged("ReasonCodes");
        }

        public Visibility Visibility
        {
            get { return _visibility; }
            set
            {
                _visibility = value;
                OnPropertyChanged("Visibility");
            }
        }

        public bool validate()
        {
            bool success = false;
            string err_msg;

            success = _model.validateOperator(OperatorCode, out err_msg, out _operator);

            ValidationError = err_msg;
            return success;
        }

        #endregion


    }
}
